package com.size.mall.manager.pms_base.controller;

import cn.hutool.core.map.MapUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.size.mall.mallapi.entity.*;
import com.size.mall.mallapi.service.*;
import com.size.mall.manager.pms_base.fastdfs.FastDFSClient;
import com.size.mall.manager.pms_base.fastdfs.FastDFSFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@CrossOrigin
public class SpuController {

    @Reference
    PmsProductInfoService spuService;


    @GetMapping("/spuList")
    public List<PmsProductInfo> getSpuList(String catalog3Id) {

        return spuService.spuList(catalog3Id);

    }

    @PostMapping(value = "/saveSpuInfo")
    public String saveSpuInfo(@RequestBody PmsProductInfo pmsProductInfo) {
        boolean is_save = spuService.save(pmsProductInfo);
        if (!is_save) {
            return "faild";
        }


        return "success";
    }

    @Reference
    PmsBaseSaleAttrService baseSaleAttrService;


    @PostMapping("/baseSaleAttrList")
    public List<PmsBaseSaleAttr> baseSaleAttrList() {

        return baseSaleAttrService.list(null);
    }


    @PostMapping("/fileUpload")
    public String fileUpload(@RequestParam("file") MultipartFile file) throws IOException {


        String url = saveFile(file);
        return url;
    }

    /**
     * @param multipartFile
     *
     * @return
     *
     * @throws IOException
     */
    public String saveFile(MultipartFile multipartFile) throws IOException {
        String[] fileAbsolutePath = {};
        String fileName = multipartFile.getOriginalFilename();
        String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
        byte[] file_buff = null;
        InputStream inputStream = multipartFile.getInputStream();
        if (inputStream != null) {
            int len1 = inputStream.available();
            file_buff = new byte[len1];
            inputStream.read(file_buff);
        }
        inputStream.close();
        FastDFSFile file = new FastDFSFile(fileName, file_buff, ext);
        try {
            fileAbsolutePath = FastDFSClient.upload(file);  //upload to fastdfs
        } catch (Exception e) {
        }
        if (fileAbsolutePath == null) {
        }
        String path = FastDFSClient.getTrackerUrl() + fileAbsolutePath[0] + "/" + fileAbsolutePath[1];
        return path;
    }

    @Reference

    PmsProductSaleAttrService pmsProductSaleAttrService;
    @Reference
    PmsProductSaleAttrValueService pmsProductSaleAttrValueService;


    @GetMapping("/spuSaleAttrList")
    public List<PmsProductSaleAttr> spuSaleAttrList(String spuId) {


        List<PmsProductSaleAttr> attrs =
                (List<PmsProductSaleAttr>) pmsProductSaleAttrService.listByMap(MapUtil.of("product_id", spuId));


        for (PmsProductSaleAttr attr : attrs) {

            String attrId = attr.getSaleAttrId();

            List<PmsProductSaleAttrValue> attrValues =
                    (List<PmsProductSaleAttrValue>) pmsProductSaleAttrValueService.listByMap(MapUtil.of("sale_attr_id"
                            , attrId));

            attr.setSpuSaleAttrValueList(attrValues);


        }

        return attrs;
    }

    @Reference
    PmsProductImageService pmsProductImageService;

    @GetMapping("/spuImageList")
    public List<PmsProductImage> spuImageList(String spuId) {


        List<PmsProductImage> productImages = (List<PmsProductImage>) pmsProductImageService.listByMap(MapUtil.of(
                "product_id", spuId));


        return productImages;
    }

}
